﻿#include "TBaseWindow.hpp"
#include <QMouseEvent>
#include <QGuiApplication>
#include <QDebug>
#include <QPainter>
#include <Theme/Config.hpp>

namespace TDesign
{
    TBaseWindow::TBaseWindow(QWidget *parent)
        : QWidget(parent)
    {
        setWindowFlag(Qt::FramelessWindowHint, true);
        setAttribute(Qt::WA_Hover);
        setMouseTracking(true);
    }

    TBaseWindow::~TBaseWindow() = default;

    void TBaseWindow::mousePressEvent(QMouseEvent *event)
    {
        if (!flag_dragging && event->button() == Qt::LeftButton)
        {
            press_pos = event->pos();
            flag_dragging = true;
            QGuiApplication::setOverrideCursor(Qt::ClosedHandCursor);
        }
        QWidget::mousePressEvent(event);
    }

    void TBaseWindow::mouseReleaseEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton)
        {
            flag_dragging = false;
            QGuiApplication::restoreOverrideCursor();
        }
        QWidget::mouseReleaseEvent(event);
    }

    void TBaseWindow::mouseMoveEvent(QMouseEvent *event)
    {
        if (flag_dragging && (event->buttons() & Qt::LeftButton))
        {
            QPoint current_pos = event->globalPos();
            if (windowState() == Qt::WindowNoState)
            {
                move(current_pos - press_pos);
            }
        }
        QWidget::mouseMoveEvent(event);
    }

    void TBaseWindow::paintEvent(QPaintEvent *event)
    {
        QPainter painter{this};
        painter.fillRect(rect(), QColor{Theme::ColorsEnumToHexStr(static_cast<uint32_t>(Theme::Light::Colors::bg_color_page)).c_str()});
    }
}
